package com.duoec.dw.lineage.service.impl.analyser.alert;

import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn;
import com.duoec.dw.lineage.core.utils.AnalyserUtils;
import com.duoec.dw.lineage.dto.DdlChangeItemDto;
import com.duoec.dw.lineage.enums.DdlChangeTypeEnum;
import com.duoec.dw.lineage.service.AlterTableItemAnalyser;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xuwenzhen
 */
@Service
public class AlterTableChangeColumnAnalyser implements AlterTableItemAnalyser<MySqlAlterTableChangeColumn> {
    /**
     * 获取处理类型
     *
     * @return SQLAlterTableItem子类
     */
    @Override
    public Class<MySqlAlterTableChangeColumn> getType() {
        return MySqlAlterTableChangeColumn.class;
    }

    /**
     * 解析变更条目，返回影响数仓的变更
     *
     * @param item 一条变更条目
     * @return 影响数仓的变更，如果没有影响则返回null
     */
    @Override
    public List<DdlChangeItemDto> analyse(MySqlAlterTableChangeColumn item) {
        String orgColumn = AnalyserUtils.formatFieldName(item.getColumnName().getSimpleName());
        DdlChangeItemDto ddlChange = new DdlChangeItemDto();
        ddlChange.setChangeType(DdlChangeTypeEnum.CHANGE_COLUMN_NAME.getType());
        ddlChange.setFieldName(orgColumn);
        return Lists.newArrayList(ddlChange);
    }
}
